var db = require('../config/db')
var sql = require('./dbsql/roleSql')

/**
 * 角色服务层
 * Author: 梦里小白龙 <jiangwenshu1990@126.com> <http://www.jz07.cn>
 */

 //添加角色
const save = (value =>{
    return  new Promise(( resolve, reject ) => {
        db( sql.insert, value).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data);
                }
        })
    })
})

//更新数据
const update = ( params => {
    let res = [ params.role_name , params.stats , params.role_txt , params.role_module , params.id ];
    return  new Promise(( resolve, reject ) => {
        db( sql.updateRoles, res ).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data);
                }
        })
    })
})

//查询角色信息
const getById = ( id => {
    return  new Promise(( resolve, reject ) => {
        db( sql.getById, id ).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data);
                }
        })
    })
})

//删除数据
const udelete = ( id => {
    return  new Promise(( resolve, reject ) => {
        db( sql.deleteRoles, id ).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data);
                }
        })
    })
})

/**
 *  查询所有用户是否存在改角色
 *  作用：删除角色是判断用户是否有该角色
 */
const selectUserRole = ( id => {
    return  new Promise(( resolve, reject ) => {
        db( sql.selectUserRole, id ).then(data=>{
                if(!data){
                    reject( err )
                }else{
                    resolve(data);
                }
        })
    })
})

module.exports = {
    save,
    update,
    udelete,
    getById,
    selectUserRole
}

